package com.yl.order.ui.dialog;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import com.yl.common.ui.BaseDialogFragment;
import com.yl.order.R;
import com.yl.order.db.entity.GoodsEntity;
import com.yl.order.db.entity.SpecEntity;
import com.yl.order.interfaces.Callback;
import com.yl.order.ui.fragment.SubSpecSelectFragment2;

import java.util.ArrayList;
import java.util.HashSet;

/**
 * create by cjx on 2024/6/5
 * desc:
 */
public class SpecSelectDialogFragment2 extends BaseDialogFragment implements View.OnClickListener {

    private GoodsEntity goods;
    private ArrayList<HashSet<String>> specSetList;
    private HashSet<String> defaultSpec;
    private Callback<ArrayList<SpecEntity>> specSelectCallback;

    private SubSpecSelectFragment2 fragment;

    public static SpecSelectDialogFragment2 newInstance(GoodsEntity goods, ArrayList<HashSet<String>> specSetList,
                                                        HashSet<String> defaultSpec, Callback<ArrayList<SpecEntity>> callback) {
        SpecSelectDialogFragment2 fragment2 = new SpecSelectDialogFragment2();
        fragment2.goods = goods;
        fragment2.specSetList = specSetList;
        fragment2.defaultSpec = defaultSpec;
        fragment2.specSelectCallback = callback;
        return fragment2;
    }

    @Override
    public int getViewId() {
        return R.layout.dialog_fragment_container;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if (specSetList == null || goods == null) {
            dismiss();
            return;
        }
        findViewById(R.id.close).setOnClickListener(v -> dismiss());

        View confirmView = findViewById(R.id.confirm);
        confirmView.setVisibility(View.VISIBLE);
        confirmView.setOnClickListener(this);

        TextView titleView = findViewById(R.id.title);
        titleView.setText(goods.getName());
        fragment = SubSpecSelectFragment2.newInstance()
                .setSpecSetList(specSetList)
                .setDefaultSpec(defaultSpec)
                .setSelectChangedCallback(list -> {

                });
        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.container, fragment);
        fragmentTransaction.commitNow();
    }

    @Override
    public void onClick(View v) {
        if (specSelectCallback != null) {
            specSelectCallback.callback(fragment.getSelectSpec());
        }
        dismiss();
    }
}
